package com.example.bigevent.controller;

import com.example.bigevent.pojo.Result;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Paths;
import java.util.UUID;

@RestController
public class FileUploadController {

    private static final String UPLOAD_DIR = "uploads/";

    @Value("${server.port}")
    private String port;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        //把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的,从而防止文件覆盖
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        file.transferTo(new File("D:\\upload\\"+filename));
        //String url = AliOssUtil.uploadFile(filename,file.getInputStream());
        //String url = "图片的地址";
        // 返回客户端 文件地址 URL
        String url = "localhost:" + port +"/upload/" + filename;
        return Result.success(url);
    }
}
